﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class MasGenderDAL : IMasGenderDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public MasGenderDAL()
        {

        }
        //public MasGenderInfo GetMasGenderInfo(Nullable<int> seq)
        //{
        //    string strSQL;

        //    MasGenderInfo Info = null;

        //    try
        //    {
        //        dbManager.Open();
        //        dbManager.CreateParameters(2);

        //        dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
        //        dbManager.AddParameters(1, "@seq", seq);

        //        strSQL = "SELECT GENDER_SEQ, GENDER_NAME, GENDER_NAME_ENG, GENDER_THAI"
        //            + " FROM MAS_GENDERS"
        //            + " WHERE RECORD_STATUS not like @DeleteRecord"
        //                + " AND GENDER_SEQ = @seq";

        //        IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

        //        while (rdr.Read())
        //        {
        //            Info.Sequence = rdr.GetInt32(0);
        //            Info.Name = rdr.IsDBNull(1) ? "" : rdr.GetString(1);
        //            Info.NameEng = rdr.IsDBNull(2) ? "" : rdr.GetString(2);
        //            Info.NameThai = rdr.IsDBNull(3) ? "" : rdr.GetString(3);
        //        }

        //        return Info;
        //    }
        //    catch
        //    {
        //        return null;
        //        throw;
        //    }
        //    finally
        //    {
        //        dbManager.CloseReader();
        //        dbManager.Dispose();
        //    }
        //}
        public IList<MasGenderInfo> ListMasGenders(MasGenderInfo gdInfo)
        {
            string strSQL;

            List<MasGenderInfo> infoList = new List<MasGenderInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Seq", gdInfo.Sequence);

                strSQL = "SELECT GENDER_SEQ, GENDER_NAME, NAME_ENG, THAI"
                   + " FROM MAS_GENDERS"
                   + " WHERE RECORD_STATUS not like @DeleteRecord"
                   + (gdInfo.Sequence > 0 ? " AND GENDER_SEQ = @Seq" : "");

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    MasGenderInfo Info = new MasGenderInfo(rdr.GetInt32(0)
                            , rdr.IsDBNull(1) ? "" : rdr.GetString(1)
                            , rdr.IsDBNull(2) ? "" : rdr.GetString(2)
                         //   , rdr.IsDBNull(3) ? "" : rdr.GetString(3)
                                     , rdr.IsDBNull(3) ? "" : rdr.GetString(3)
                            , null, null);
                    infoList.Add(Info);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("{class MasGenderDAL}:" + ex.Message); 
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
            return infoList;
        }

        public void Insert(MasGenderInfo info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@Name", info.Name);
                dbManager.AddParameters(1, "@NameEng", info.NameEng);
                dbManager.AddParameters(2, "@NameThai", info.NameThai);
                dbManager.AddParameters(3, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(4, "@CreatedBy", info.CreatedBy);
                dbManager.AddParameters(5, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO MAS_GENDERS(GENDER_NAME, GENDER_NAME_ENG, GENDER_THAI"
                        + ", RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@Name, @NameEng, @NameThai, @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(MasGenderInfo info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@Seq", info.Sequence);
                dbManager.AddParameters(1, "@Name", info.Name);
                dbManager.AddParameters(2, "@NameEng", info.NameEng);
                dbManager.AddParameters(3, "@NameThai", info.NameThai);
                dbManager.AddParameters(4, "@UpdatedBy", info.UpdatedBy);
                dbManager.AddParameters(5, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE MAS_GENDERS"
                           + " SET GENDER_NAME = @Name"
                                + ", GENDER_NAME_ENG = @NameEng"
                                + ", GENDER_THAI = @CountrySeq"
                                + ", UPDATED_BY = @UpdatedBy"
                                + ", UPDATED_WHEN = @UpdatedWhen"
                           + " WHERE GENDER_SEQ = @Seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(int Sequence, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@seq", Sequence);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", UpdatedBy);
                dbManager.AddParameters(3, "@updated_when", DateTime.Today);

                strSQL = "UPDATE MAS_GENDERS"
                    + " SET RECORD_STATUS = @record_status"
                        + ", UPDATED_BY = @updated_by"
                        + ", UPDATED_WHEN = @updated_when"
                    + " WHERE GENDER_SEQ = @seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
